private var playerInfo:AIObject;
var playerTower:Tower;
public var oppPlayer:PlayerObject;
var oppTower:Tower;
var blockMaker:InstantiateBlocks;
var ui: UIHandler;

private var randomGen:Random=new Random();

function Start(){
	playerInfo=this.gameObject.GetComponent("AIObject");
	}

//slot Array order PlayerNum:PlayerSlot:AbilityName:EgoDrain:Effect:Power:Duration:Type
function ExecuteMove(inButton:String)
{
	
	if(inButton==""){
		ui.ShowMessage(playerInfo.aiName + " is waiting...",5);
	}
	else{
		var slotNum=findSlot(inButton);
		if (slotNum==0){
			chargeCheck=ui.GetChargeNum(playerInfo.teamNum,inButton);
			egoCheck= EgoCheck(inButton);
			if(chargeCheck==2 && egoCheck){
				blockMaker.MakeBlock(playerTower,inButton);
				ui.LoseCharge(playerInfo.teamNum,inButton);
				ui.LoseCharge(playerInfo.teamNum,inButton);
				if(inButton=="Logic" || inButton=="Ethics"){
					playerInfo.DecreaseEgo(40);
				}
				else if(inButton=="Passion"){
					playerInfo.DecreaseEgo(20);
				}
				else{
					playerInfo.DecreaseEgo(60);
				}
				ui.ShowMessage(playerInfo.aiName + " created a " + inButton + " block!",3);
			}
			else{
				ui.ShowMessage(playerInfo.aiName + " didn't have enough charge to create the Block!",3);
			}
		}
		else{	
			
			var curSlot=playerInfo.slots[slotNum-1];
			//Debug.Log(curSlot);
			//Debug.Log(curSlot[4]);
			var egoCost=parseInt(curSlot[3]);
			if (playerInfo.currentEgo>=egoCost){
				playerInfo.DecreaseEgo(egoCost);
				var attackName=curSlot[2];
				var attackEffect=curSlot[4];
				var attackPower=parseInt(curSlot[5]);
				attackPower=checkForCounter(attackPower);
				var effectDuration=parseInt(curSlot[6]);
				var attackType=curSlot[7];
				ui.AddCharge(playerInfo.teamNum,attackType);
				
				if (attackEffect=="Normal Attack" && attackType != "Logic")
				{
					attackPower=checkForTrap(attackPower);
					oppTower.DamageTopBlock(attackPower);
					ui.ShowMessage(playerInfo.aiName + " did " + attackPower 
						+ " damage to the opponent's tower with "+ attackName + "!", 3);
				}
				if (attackEffect=="Normal Attack" && attackType=="Logic")
				{
					check=parseInt(randomGen.value*100);
					if(check%2==0){
						oppTower.DamageTopBlock(attackPower);
					}
					else{
						oppTower.DamageBottomBlock(attackPower);
					}
					ui.ShowMessage(playerInfo.aiName + " did " + attackPower 
						+ " damage to the opponent's tower with "+ attackName + "!", 3);
				}
				else if (attackEffect=="Ego Vampire")
				{
					oppPlayer.DecreaseEgo(attackPower);
					playerInfo.AddEgo(attackPower);
					ui.ShowMessage(playerInfo.aiName + "stole " + attackPower
						+ " Ego with " +attackName +"!", 3);
				}
				
				else if (attackEffect=="Ego Burn")
				{
					oppPlayer.DecreaseEgo(attackPower);
					ui.ShowMessage(playerInfo.aiName + "burned " + attackPower
						+ " Ego with " +attackName +"!", 3);
				}
				
				else if(attackEffect=="Ego Boost")
				{
					playerInfo.AddEgo(attackPower);
					ui.ShowMessage(playerInfo.aiName + "gained " + attackPower
						+ " Ego with " +attackName +"!", 3);
				}
				else if(attackEffect=="Charge Boost"){
					var typeArr=["Passion","Logic","Deception","Ethics"];
					var chosenSlot= parseInt((randomGen.value*100)%4);
					ui.AddCharge(playerInfo.teamNum,typeArr[chosenSlot]);
					ui.ShowMessage(playerInfo.aiName + "got a " + typeArr[chosenSlot]
						+ " Charge with " +attackName +"!", 3);
				}
				else if(attackEffect=="Sacrifice Attack"){
					playerTower.DamageTopBlock(10);
					oppTower.DamageTopBlock(attackPower);
					ui.ShowMessage(playerInfo.aiName + " sacrificed height to do " + attackPower +
						" damage to the opposing Tower.",3);
				}
				else if(attackEffect=="Charge Drain"){
					var typeArray=["Passion","Logic","Deception","Ethics"];
					var choseSlot= parseInt((randomGen.value*100)%4);
					ui.LoseCharge(oppPlayer.teamNum,typeArr[chosenSlot]);
					ui.ShowMessage(playerInfo.aiName + "destroyed a " + typeArray[choseSlot]
						+ " Charge with " +attackName +"!", 3);
				}
				else if(attackEffect=="Counter"){
					playerTower.setAttackCounter(attackPower);
					ui.ShowMessage(playerInfo.aiName + " cast something on himself....",3);
				}
				else if(attackEffect=="Trap"){
					playerTower.setTrap(attackPower);
					ui.ShowMessage(playerInfo.aiName + " cast something on himself....",3);
				}
			}
			else{
				ui.SendMessage(playerInfo.aiName + "doesn't have enough ego to use that move.",3);
			}
		}
	}
}

function checkForCounter(atkPower:int){
	if(oppTower.attkCounter==0){
		return atkPower;
	}
	else if(oppTower.attkCounter==50){
		oppTower.setAttackCounter(0);
		return (atkPower/2);
	}
	else{
		oppTower.setAttackCounter(0);
		return 0;
	}
}

function EgoCheck(inType:String){
	if((inType=="Logic" || inType=="Ethics")&& playerInfo.currentEgo>40){
		return true;
	}
	else if(inType=="Passion" && playerInfo.currentEgo>20){
		return true;
	}
	else if(inType=="Deception" && playerInfo.currentEgo>60){
		return true;
	}
	else{
		return false;
	}
}
function checkForTrap(atkPower:int){
	if(oppTower.trap==0){
		return atkPower;
	}
	else{
		atkPower=atkPower/2;
		playerTower.DamageTopBlock(atkPower);
		oppTower.setTrap(0);
		return atkPower;
	}
}

function findSlot(inButton:String){
	if(inButton=="11" || inButton=="21"){
		return 1;
	}
	else if(inButton=="12" || inButton=="22"){
		return 2;
	}
	else if(inButton=="13" || inButton=="23"){
		return 3;
	}
	else if(inButton=="14" || inButton=="24"){
		return 4;
	}
	else if(inButton=="15" || inButton=="25"){
		return 5;
	}
	else{
		return 0;
	}
}
	
	
	
	
	

	